@echo off
setlocal
echo Setting environment path...
set "ROOT=%~dp0cfclient_Anaconda"

:: Environment validation
if not exist "%ROOT%\python.exe" (
    echo Error: Python not found in %ROOT%
    pause
    exit /b 1
)

:: Path cleaning and priority adjustment
set "CLEANED_PATH=%PATH:Anaconda3=%"
set "CLEANED_PATH=%CLEANED_PATH:WindowsApps=%"
set "PATH=%ROOT%;%ROOT%\Scripts;%ROOT%\Library\bin;%ROOT%\DLLs;%ROOT%\Library\mingw-w64\bin;%CLEANED_PATH%"

rem Set Python environment
set "PYTHONHOME=%ROOT%"
set "PYTHONPATH=%ROOT%\Lib;%ROOT%\Lib\site-packages"

:: Debugging commands
echo [DEBUG] Python path verification
echo Expected path: %ROOT%\python.exe
where python
"%ROOT%\python.exe" --version

echo Executing CFClient...
start "" /wait "%ROOT%\python.exe" -m cfclient.gui || (
    echo [ERROR] Launch failed with code: %errorlevel%
    pause
    exit /b %errorlevel%
)

pause
endlocal